package com.ruoyi.course.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.course.domain.CourseLesson;
import com.ruoyi.course.dto.CourseScheduleQueryDto;
import com.ruoyi.course.service.ICourseLessonService;
import com.ruoyi.course.vo.CourseScheduleVo;
import com.ruoyi.course.vo.SelectOptionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 课表Controller
 * 
 * @author ruoyi
 * @date 2025-06-12
 */
@RestController
@RequestMapping("/course/courseLesson")
public class CourseLessonController extends BaseController
{
    @Autowired
    private ICourseLessonService courseLessonService;

    /**
     * 查询课表列表
     */
    @PreAuthorize("@ss.hasPermi('course:courseLesson:list')")
    @GetMapping("/list")
    public TableDataInfo list(CourseLesson courseLesson)
    {
        startPage();
        List<CourseLesson> list = courseLessonService.selectCourseLessonList(courseLesson);
        return getDataTable(list);
    }

    /**
     * 导出课表列表
     */
    @PreAuthorize("@ss.hasPermi('course:courseLesson:export')")
    @Log(title = "课表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CourseLesson courseLesson)
    {
        List<CourseLesson> list = courseLessonService.selectCourseLessonList(courseLesson);
        ExcelUtil<CourseLesson> util = new ExcelUtil<CourseLesson>(CourseLesson.class);
        util.exportExcel(response, list, "课表数据");
    }

    /**
     * 获取课表详细信息
     */
    @PreAuthorize("@ss.hasPermi('course:courseLesson:query')")
    @GetMapping(value = "/{lessonId}")
    public AjaxResult getInfo(@PathVariable("lessonId") Integer lessonId)
    {
        return success(courseLessonService.selectCourseLessonByLessonId(lessonId));
    }

    /**
     * 新增课表
     */
    @PreAuthorize("@ss.hasPermi('course:courseLesson:add')")
    @Log(title = "课表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CourseLesson courseLesson)
    {
        return toAjax(courseLessonService.insertCourseLesson(courseLesson));
    }

    /**
     * 修改课表
     */
    @PreAuthorize("@ss.hasPermi('course:courseLesson:edit')")
    @Log(title = "课表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CourseLesson courseLesson)
    {
        return toAjax(courseLessonService.updateCourseLesson(courseLesson));
    }

    /**
     * 删除课表
     */
    @PreAuthorize("@ss.hasPermi('course:courseLesson:remove')")
    @Log(title = "课表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{lessonIds}")
    public AjaxResult remove(@PathVariable Integer[] lessonIds)
    {
        return toAjax(courseLessonService.deleteCourseLessonByLessonIds(lessonIds));
    }

    /**
     * 获取校区选项列表
     */
    @PreAuthorize("@ss.hasPermi('course:courseLesson:list')")
    @GetMapping("/campus/options")
    public AjaxResult getCampusOptions()
    {
        List<SelectOptionVo> options = courseLessonService.getCampusOptions();
        return success(options);
    }

    /**
     * 根据校区id获取班级选项列表
     */
    @PreAuthorize("@ss.hasPermi('course:courseLesson:list')")
    @GetMapping("/class/options/{campusId}")
    public AjaxResult getClassOptions(@PathVariable("campusId") Integer campusId)
    {
        List<SelectOptionVo> options = courseLessonService.getClassOptionsByCampusId(campusId);
        return success(options);
    }

    /**
     * 获取课程表日历数据
     */
    @PreAuthorize("@ss.hasPermi('course:courseLesson:list')")
    @PostMapping("/schedule/calendar")
    public AjaxResult getCourseScheduleCalendar(@RequestBody CourseScheduleQueryDto queryDto)
    {
        CourseScheduleVo scheduleVo = courseLessonService.getCourseScheduleCalendar(queryDto);
        return success(scheduleVo);
    }
}
